package code.ws_common_scene.mode.base
{
	import code.ws_common_com.core.Progress;
	import code.ws_common_ems.ModeActionEms;
	import code.ws_common_ems.ModeStatusEms;
	import code.ws_common_scene.data.AtkData;
	import code.ws_common_scene.data.HpDamage;
	import code.ws_common_scene.data.base.Damage;
	import code.ws_common_scene.manager.SceneArticleManager;
	import code.ws_common_scene.manager.SceneManager;
	import code.ws_common_utils.LayerUtils;
	
	import feathers_exd.ctrls.ULabel;
	
	import flash.geom.Rectangle;
	
	import starling.core.Starling;

	/**
	 * 战斗模型
	 * @author qiangchen
	 * 
	 */	
	public class Fighter extends BRole
	{
		/** 攻击值 */
		private var _power:int;
		/** 最大血量值 */
		private var _maxHp:int;
		/** 当前血量值 */
		private var _hp:int;
		/** 血条 */
		private var _blood:Progress=null;
		/** 伤害数据 */
		protected var _attackData:AtkData;
		
		
		/**
		 * 构造函数 
		 * @param render
		 * @param style
		 * @param action
		 * @param dir
		 * 
		 */		
		public function Fighter( render:String, style:String, action:String, dir:int )
		{
			super( render, style, action, dir );
			this.touchable = false;
			
			//初始化血条
			_blood = new Progress( "rBlood_bg", "rBlood", new Rectangle( 52, 7, 1, 1 ), new Rectangle( 66, 4, 1, 1 ) );
			_blood.touchable = false;
			_blood.setValue( 1 );
			_blood.height -= 2;
			_blood.x = - _blood.width >> 1;
			topCons.addChild( _blood );
			
			// 设置头顶位置
			setTopConsInnerPos();
		}
		
		/*---------------------------------------properties----------------------------------------*/
		
		/**
		 * 设置攻击值 
		 * @param val
		 * 
		 */		
		public function set power( val:int ):void
		{
			val = ( val <= 0 ) ? 1 : val;
			_power = val;
		}
		public function get power():int{return _power};
		
		/**
		 * 设置最大血量 
		 * @param val
		 * 
		 */		
		public function set maxHp( val:int ):void{_maxHp = val};
		public function get maxHp():int{return _maxHp};
		
		
		/**
		 * 设置当前血量 
		 * @param val
		 * 
		 */		
		public function set hp( val:int ):void{_hp = val};
		public function get hp():int{return _hp};
		
		
		/*-------------------------------------------------------End---------------------------------------------------*/
		
		
		/**
		 * 受伤 
		 * @param d 伤害包
		 */		
		public function hurt( d:Damage ):void
		{
			if( d == null ) return;
			
			/* ---血伤害 ---*/
			if( d._type == Damage.HURT_HP )
			{
				// 伤害飘字
				var hurtVal:String =  (d as HpDamage)._count.toString();
				var hurtInfos:String = ( (d as HpDamage).isCrit ) ? "Crit  -"  : "-" ;
				var ulabel:ULabel = new ULabel( hurtInfos + hurtVal  );
//				ulabel.filter = BlurFilter.createGlow( 0x000000, 1, 0.2 );
				ulabel.changeBold( true );
				ulabel.changeSize( 30 );
				ulabel.changeTextColor( 0xf20404 );
				LayerUtils.scene.addChild( ulabel );
				ulabel.validate();
				ulabel.x = this.x - (ulabel.width >> 1);
				ulabel.y = this.y + this.topCons.y - ulabel.height - 5;
				Starling.juggler.tween( ulabel, 0.6, { y: ulabel.y - 40, onComplete:function():void{ 
					Starling.juggler.removeTweens( ulabel );
					ulabel.dispose(); 
					ulabel.parent.removeChild( ulabel , true ); 
					ulabel = null 
				}  
				} );
				hurtVal = null;
				hurtInfos = null;
				
				// 添加命中特效
				if( (d as HpDamage).isCrit )
				{
					this.addEfts( "hit", "attackHit" );
				}
				
				// 死亡
				if( hp <= (d as HpDamage)._count )
				{
					
					_blood.setValue( 0 );
					d.dispose();
					d = null;
					
					dead();
					return;
				}
				
				hp -= (d as HpDamage)._count;
				var hpPer:Number = hp / maxHp;
				_blood.setValue( hpPer );
				hpPer = NaN;
				
				// 播放受伤动作
				if( this.showid != SceneManager.instance.scene.hero.showid && this.status == ModeStatusEms.STAND )
				{
					this.setStatus( ModeStatusEms.HURT );
					this.setAction( ModeActionEms.HURT, null, hurtOverHandler );
				}
			}
			
			// dispose
			d.dispose();
			d = null;
		}
		
		
		private function hurtOverHandler():void
		{
			if( this.status == ModeStatusEms.HURT )
			{
				stand();
			}
		}
		
		
		/**
		 * 死亡 
		 * 
		 */		
		public function dead():void
		{
			if( this.status == ModeStatusEms.DEAD )
			{
				return;
			}
			
			this.setStatus( ModeStatusEms.DEAD );
			this.setAction( ModeActionEms.DEAD, null, deadActionPlayCom );
			
			// 隐藏头顶容器
			this.topCons.visible = false;
		}
		
		private function deadActionPlayCom():void
		{
//			trace( "死亡者：" +  this.thisName );
			Starling.juggler.tween( this, 2, {alpha:0, onComplete:SceneArticleManager.instance.removeArticle, onCompleteArgs:[this.showid]} );
		}
		
		
		/**
		 * 攻击 
		 * @param data 攻击数据
		 * 
		 */		
		public function attack( data:AtkData ):void
		{
			if( status == ModeStatusEms.ATTACK )
			{
				return;
			}
			setStatus( ModeStatusEms.ATTACK );
			setAction( ModeActionEms.ATTACK, attackUpdata, attackKey );
			
			// 赋值
			_attackData = data;
			data = null;
		}
		
		
		/**
		 * 攻击过程中 （子类重写）
		 * 
		 */		
		protected function attackUpdata():void{};
		
		
		/**
		 * 攻击关键帧 （子类可继承重写。警告：在子类中先super.attackKey()后再续写后续内容）
		 * 
		 */		
		protected function attackKey():void
		{
//			trace("【ws_mobile】---> attack over!");
			stand();
			// ask and go attack damage
			if( _attackData )
			{
				SceneArticleManager.instance.hurt( _attackData );
				_attackData.dispose();
				_attackData = null;
			}
		}
		
		
		/**
		 * 释放 
		 * 
		 */		
		override public function dispose():void
		{
			if( _attackData )
			{
				_attackData.dispose();
				_attackData = null;
			}
			
			if( _blood )
			{
				_blood.dispose();
				_blood.parent && _blood.parent.removeChild( _blood, true );
				_blood = null;
			}
			_power = NaN;
			_hp = NaN;
			super.dispose();
		}
	}
}